package com.woniu.controller;

import cn.dev33.satoken.util.SaResult;
import com.alibaba.fastjson2.JSON;
import com.woniu.entity.AllUser;
import com.woniu.service.RegisterService;
import com.woniu.utils.UploadMinioUtil;
import jakarta.validation.Valid;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.codec.multipart.FilePart;
import org.springframework.web.bind.annotation.*;

/**
 * @Author hq
 * @Date 2025/6/19$ 23:03$
 * @Description TODO
 * @Params $
 * @Return $
 **/

@RestController
@RequestMapping("/register")
public class    RegisterController {
    @Autowired
    private RegisterService registerService;
    @Autowired
    private UploadMinioUtil uploadMinioUtil;
    //上传头像到minio
    @PostMapping("/uploadAvatar")
    public SaResult uploadAvatar( @RequestPart("file") FilePart filePart) {
        return SaResult.ok().setData(uploadMinioUtil.addDocuments(filePart));
    }
    @PostMapping("/add")
    public SaResult add(
            @RequestPart("user") String userJson,
            @RequestPart(name = "file", required = false) FilePart filePart
    ) {
        // 将 JSON 字符串转换为 AllUser 对象
        AllUser allUser = JSON.parseObject(userJson, AllUser.class);

        // 打印接收到的参数（调试用）
        System.out.println("Received user: " + allUser);
        if (filePart != null) {
            System.out.println("Received file: " + filePart.filename());
        }

        // 处理头像上传
        if (filePart != null) {
            String avatarUrl = uploadMinioUtil.addDocuments(filePart).block();
            allUser.setAvatarUrl(avatarUrl);
        } else {
            allUser.setAvatarUrl(""); // 没有上传头像则设为空
        }

        return SaResult.ok().setData(registerService.add(allUser));
    }



    //检查电话唯一性
    @GetMapping ("/checkContact")
    public SaResult checkContact(@RequestParam String contact){
        return SaResult.ok().setData(registerService.checkContact(contact));
    }
    //检查用户名唯一性
    @GetMapping("/checkUsername")
    public SaResult checkUsername(@RequestParam String username){
        return SaResult.ok().setData(registerService.checkUsername(username));
    }
}
